package com.example.localareanetworkchat.fragment.home

import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.fragment.app.Fragment
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.localareanetworkchat.R
import com.example.localareanetworkchat.fragment.MainViewModel
import com.example.localareanetworkchat.fragment.home.adapter.ChatMessageListAdapter
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.fragment_message.*

class MessageFragment : Fragment() {
    val TAG = MessageFragment::class.simpleName
    var viewModel: MainViewModel ?= null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_message, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(requireActivity(), ViewModelProvider.AndroidViewModelFactory(requireActivity().application)).get(
            MainViewModel::class.java)
        val chatGroup = viewModel?.chatGroup!!
        requireActivity().findViewById<Toolbar>(R.id.toolbar).title = chatGroup.groupName
        requireActivity().findViewById<BottomNavigationView>(R.id.bnv_chat_main).visibility = View.GONE

        requireActivity().findViewById<Toolbar>(R.id.toolbar).setNavigationIcon(R.drawable.ic_baseline_keyboard_backspace_24)
        val navController = Navigation.findNavController(requireView())
        requireActivity().findViewById<Toolbar>(R.id.toolbar).setNavigationOnClickListener {
            navController.navigate(R.id.homeFragment)
        }

        requireActivity().findViewById<Toolbar>(R.id.toolbar).inflateMenu(R.menu.menu_chat_message)
        requireActivity().findViewById<Toolbar>(R.id.toolbar).setOnMenuItemClickListener {
            when(it.itemId){
                R.id.menu_item_chat_message_group_info -> {
                    Navigation.findNavController(requireView()).navigate(R.id.chatGroupInfoFragment)
                    true
                }
                else -> true
            }
        }

        val chatMessageListAdapter = ChatMessageListAdapter(requireContext())
        val rvLayoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, true)
        rv_chat_message.layoutManager = rvLayoutManager
        rvLayoutManager.stackFromEnd = true
        rv_chat_message.adapter = chatMessageListAdapter

        viewModel?.queryAllChatMessageByChatGroupId(chatGroup.chatGroupId)?.observe(requireActivity(), {
            chatMessageListAdapter.submitList(viewModel?.getChatMessageItemList(viewModel?.user!!, it))
            rvLayoutManager.scrollToPosition(0)
        })

        btn_chat_message_send.setOnClickListener {
            val message = et_chat_message_content.text.toString().trim()
            if (message.isEmpty() || message.isBlank()) return@setOnClickListener

            viewModel?.sendMessage(viewModel?.user!!, chatGroup, message)
            et_chat_message_content.text.clear()

        }

    }

}